package com.qtummatrix.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.opensymphony.xwork2.ActionSupport;
import com.qtummatrix.entity.*;
import com.qtummatrix.service.ILoansService;
import com.sun.deploy.net.HttpResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jsl
 * @date 2019/11/26 - 10:49
 */
@Controller("LoansAction")
public class LoansController extends ActionSupport {

    @Autowired
    @Qualifier("loansService")
    private ILoansService iLoansService;

    private Map<String, Object> info = new HashMap<String, Object>();
    private CustomerinfoEntity customerinfoEntity = new CustomerinfoEntity();

    public CustomerinfoEntity getCustomerinfoEntity() {
        return customerinfoEntity;
    }

    public void setCustomerinfoEntity(CustomerinfoEntity customerinfoEntity) {
        this.customerinfoEntity = customerinfoEntity;
    }

    private ApplicationEntity applicationEntity;

    public ApplicationEntity getApplicationEntity() {
        return applicationEntity;
    }

    public void setApplicationEntity(ApplicationEntity applicationEntity) {
        this.applicationEntity = applicationEntity;
    }

    private String name;
    private String UID;

    private String tel;
    private String code;

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUID() {
        return UID;
    }

    public void setUID(String UID) {
        this.UID = UID;
    }

    public Map<String, Object> getInfo() {
        return info;
    }

    public void setInfo(Map<String, Object> info) {
        this.info = info;
    }

    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 查询产品详情
     *
     * @return
     */
    public String showLoans() {
        LoanproductsEntity loanproductsEntity = iLoansService.show(1);
        info.put("loans", loanproductsEntity);
        return SUCCESS;
    }

    /**
     * 我的贷款  登录
     *
     * @return
     */
    public String addLogin() {
        String str = iLoansService.loginLoans(name, UID);
        info.put("msg", str);
        return SUCCESS;
    }

    /**
     * 客户信息 登录成功后
     */

    public String getMessage() {
        CustomerinfoEntity customerinfoEntity = iLoansService.getMessage(name, UID);
        info.put("user", customerinfoEntity);
        return SUCCESS;
    }

    /**
     * 贷款信息表
     */
    public String showMessage() {
        ContractEntity contractEntity = iLoansService.getShow(name);
        info.put("show", contractEntity);
        return SUCCESS;
    }

    /**
     * 添加客户
     */
    public String addUSer() {
        String str = iLoansService.addUsers(applicationEntity);
        info.put("add", str);
        return SUCCESS;
    }

    /**
     * 查询模组
     * ***************************************
     */
    public String getQuest() {
        List<QuestionsEntity> list = iLoansService.getQuest();
        info.put("list", list);
        return SUCCESS;
    }

    /**
     * 信用评级插入到客户表
     *
     * @return
     */
    public String addRole() {
        HttpServletRequest request = ServletActionContext.getRequest();
        String arr = request.getParameter("Arr");
        JSONArray obj = JSON.parseArray(arr);
        String[] choose = new String[10];
        for (int i = 0; i < 10; i++) {
            choose[i] = (String) obj.get(i);
        }
        String str = iLoansService.getGrode1(choose, username);
        info.put("str", str);
        return SUCCESS;
    }


    public String sendMsg() {
        System.out.println(tel);
        System.out.println(code);
        String host = "http://yzxyzm.market.alicloudapi.com";
        String path = "/yzx/verifySms";
        String method = "POST";
//      "你自己的AppCode";
        String appcode = "fdf1fe5daace410986505167e4621a4e";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("phone", tel);
        querys.put("templateId", "TP18040314");
        querys.put("variable", "code:" + code);
        Map<String, String> bodys = new HashMap<String, String>();

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway
             * /demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            info.put("infocode", "1");
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
            info.put("infocode", "0");
        }
        return SUCCESS;
    }
}